package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.data.ResponseStausEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbSeckillGoods;
import com.dongdongshop.pojo.TbSeckillOrder;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.SeckillGoodsService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description
 * @ClassName SeckillController
 * @Author RC
 * @date 2020.12.30 19:06
 */
@Controller
@RequestMapping("seckill")
public class SeckillController {
    @Reference(timeout = 100000,check = false)
    private SeckillGoodsService seckillGoodsService;
    @Autowired
    private RedisTemplate redisTemplate;

    private static final Logger logger = LoggerFactory.getLogger(SeckillController.class);
    @RequestMapping("showSeckillList")
    @ResponseBody
    public DataResult showSeckillList(){
        try {
            logger.info("进入秒杀商品查询方法");
            List<TbSeckillGoods> seckillGoodsList = seckillGoodsService.showSeckillList();
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(seckillGoodsList);

        } catch (Exception e) {
            logger.info("查询失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }
    @RequestMapping("getSeckillGoodsById")
    public String getSeckillGoodsById(Long id, Model model){
        logger.info("进入秒杀商品详情页方法");
        TbSeckillGoods seckillGoods = seckillGoodsService.getSeckillGoodsById(id);
        //判断商品是否存在 不存在返回错误页面
        if(seckillGoods == null){
            return "error";
        }
        model.addAttribute("seckillGoods",seckillGoods);
        return "seckill-item";

    }
    @RequestMapping("sumitOrder")
    @ResponseBody
    public DataResult sumitOrder(Long seckillId){
            //先判断用户是否登录
            TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
            if(user == null){//提示请先登录
                return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_UNLOGIN);
            }
            try {
                logger.info("进入秒杀商品抢购方法");
                seckillGoodsService.sumitOrder(seckillId,user.getUsername());

            }catch (RuntimeException e){
                return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_FAIL).setData("商品已被抢光");
            }
            return DataResult.response(ResponseStausEnum.SUSSCESS);

    }

    @RequestMapping("findOrder")
    @ResponseBody
    public DataResult findOrder(Long seckillId){
            //先判断用户是否登录
            TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
            try {
                logger.info("进入秒杀商品抢购方法");
                TbSeckillOrder seckillOrder = seckillGoodsService.findOrderByUsername(user.getUsername());
                if(seckillOrder == null){//这里为空 有可能是订单没有生成 就来查询 导致查询失败
                    return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_FAIL);
                }else if(seckillOrder.getId().intValue() == -999){//库存扣减成功 订单生成失败
                    //订单生成失败 还原库存
                    //Long decrement = redisTemplate.boundValueOps("stockCount_" + seckillId).increment();
                    return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
                }

                return DataResult.response(ResponseStausEnum.SUSSCESS).setData(seckillOrder);
            }catch (RuntimeException e){
                return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_FAIL).setData("商品已被抢光");
            }

    }

}
